package com.AAL.DAO;

import java.util.List;

import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IRequestingDetailDAO;
import com.AAL.Model.OrderDetail;
import com.AAL.Model.RequestingDetail;
import com.AAL.Model.SourcingRequest;
import com.AAL.Model.TransferingRequest;
import com.AAL.Utils.HibernateUtil;

public class RequestingDetailDAO implements IRequestingDetailDAO {

	@Override
	public RequestingDetail saveAndUpdateRequestingDetail(
			RequestingDetail requestingDetail) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(requestingDetail);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return requestingDetail;
	}

	@Override
	public RequestingDetail deleteRequestingDetail(int requesetDetailID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			RequestingDetail requestingDetail = (RequestingDetail) session
					.load(RequestingDetail.class, requesetDetailID);
			if (null != requestingDetail) {
				session.delete(requestingDetail);
			}
			session.getTransaction().commit();
			return requestingDetail;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}

	}

	@Override
	public RequestingDetail getRequestingDetail(int requestDetailID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		RequestingDetail requestingDetail = (RequestingDetail) session.load(RequestingDetail.class, requestDetailID);
		return requestingDetail;
	}

	@Override
	public List<RequestingDetail> getLstRequestingDetails(
			SourcingRequest sourcingRequest,
			TransferingRequest transferingRequest) {
		// TODO Auto-generated method stub
		return null;
	}
	
	//Thu 6 ngay 1/4/2011
	//Huyen
	//Hien thi chi tiet yeu cau theo ma yeu cau	
	@SuppressWarnings("unchecked")
	public List<RequestingDetail>  GetRequestingDetailBySourcingRequestID(String sourcingRequestID ) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<RequestingDetail> lstrequestingDetail = null;
		
		try{
			lstrequestingDetail = (List<RequestingDetail>) session
					.createQuery(
							"from RequestingDetail where sourcingRequest.sourcingRequestID=:sourcingRequestID")
					.setParameter("sourcingRequestID",sourcingRequestID).list();
		}catch (Exception e) {
			System.out.println("E:"+e.getMessage());
			return null;
		}		
		return lstrequestingDetail;
	}
	//Phatdt hien thi chi tiet phieu dieu phoi start
	@SuppressWarnings("unchecked")
	public List<RequestingDetail> GetRequestingDetailByTransferingRequestID(
			String transferingRequestID) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<RequestingDetail> lstrequestingDetail = null;
		
		try{
			lstrequestingDetail = (List<RequestingDetail>) session
					.createQuery(
							"from RequestingDetail where transferingRequest.transferingRequestID=:transferingRequestID")
					.setParameter("transferingRequestID",transferingRequestID).list();
		}catch (Exception e) {
			System.out.println("E:"+e.getMessage());
			return null;
		}		
		return lstrequestingDetail;
	}
	//Phatdt hien thi chi tiet phieu dieu phoi end
}
